/**
 * Copyright (c) 2015-2019, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.ehcache.CacheKit;

import cn.hg.jfsa.model.Column;

/**
 * @author Golly
 */
public class ColumnService {

	/**
	 * @param org
	 */
	public void del (Column org) {
		this.del(org, true);
	}

	/**
	 * @param org
	 * @param withChildren
	 */
	public void del (Column org, boolean withChildren) {

		if (withChildren) {
			this.delWithChildren(org);
		} else {
			org.setStatus("del").update();
		}

	}

	/**
	 * @return
	 */
	public List<Column> getTree (Map<String, Object> paraMap) {
		List<Column> tree = CacheKit.get("column", "tree");

		if (null == tree) {
			List<Column> list =
				Column.DAO.find("SELECT c.* FROM t_column c WHERE c.status=? AND theme=?", "on", paraMap.get("theme"));
			tree = this.buildTree(list, 0);
			tree.add(0, new Column()
				.setId(0)
				.setName("-- 顶层 --")
				.setParentId(0)
				.setStatus("on")
				.setCreatorId(1)
				.setEditorId(1)
				.setLayerNum(0));
			CacheKit.put("column", "tree", tree);
		}

		return tree;
	}

	/**
	 * @param bean
	 */
	public void updateColumnChildredIds (Column bean) {

		if (null == bean) {
			return;
		}

		String childrenIds = Db.queryFirst("SELECT GROUP_CONCAT(id) FROM t_column WHERE status<>'del' AND parent_id=?",
			bean.getId());
		bean.setChildrenIds(childrenIds).update();
	}

	/**
	 * @param list
	 * @param parentId
	 *
	 * @return
	 */
	private List<Column> buildTree (final List<Column> list, Integer parentId) {
		List<Column> tree = new ArrayList<>();

		for (Column bean : list) {

			if (parentId.equals(bean.getParentId())) {
				tree.add(bean);
			}

		}

		for (Column bean : tree) {
			bean.setChildren(this.buildTree(list, bean.getId()));
		}

		tree.sort(new Comparator<Column>() {

			@Override
			public int compare (Column o1, Column o2) {
				return o1.getSortNum().compareTo(o2.getSortNum());
			}

		});
		return tree;
	}

	/**
	 * @param org
	 */
	private void delWithChildren (Column org) {
		org.setStatus("del").update();
		List<Column> childrenList = Column.DAO.find("SELECT * FROM t_column WHERE status<>'del' AND parent_id = ?",
			org.getId());

		for (Column child : childrenList) {
			child.setStatus("del").update();
			this.delWithChildren(child);
		}

	}

}
